<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    class Restaurentmenus extends MX_Controller
    {   
        function __construct() {
            parent::__construct();                               
            // load session library
            $this->load->library('pagination');  
            $this->load->model('Restaurentmenus_model');  
        }
        
        function index($row = 0)//+add category
        {   
            if(!($this->session->userdata('login'))) redirect('administrator/login');
            if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
            redirect('administrator/index');
            
            if($this->input->post('txttitle'))
            {
                //add Term
                $term = new Term();
                $term->name = $this->input->post('txttitle');
                $term->slug = $this->input->post('txtslug');
                
                if($term->save())
                {    
                    $last_term = new Term();
                    $last_term->where('name',$this->input->post('txttitle'))->get();                    
                    //add Term_taxonomy
                    $term_taxonomy = new Term_taxonomy();                   
                    //$term_taxonomy->term_id=$last_term->term_id;
                    $term_taxonomy->taxonomy = 'restaurentmenu';
                    $term_taxonomy->description = $this->input->post('txtexcerpt');
                    $term_taxonomy->restaurent_id = $this->input->post('restaurent_id');
                    $term_taxonomy->parent_term = $this->input->post('ddlTerm');
                    $term_taxonomy->save($last_term);
                }
            }                                    
            include('paging.php');
            $config['base_url']= base_url()."administrator/restaurentmenus/index/";
            $config['total_rows']=$this->Restaurentmenus_model->get_count_res_menu(0,5);                      		
            $config['cur_page']= $row;
            $config['num_links'] = 3;		
            $this->pagination->initialize($config);
            $data['list_link'] = $this->pagination->create_links();	                    
            $data['list_res_menu'] = $this->Restaurentmenus_model->get_res_menu(0,5,$row,$config['per_page']);
            //print_r($data['list_res_menu']);
            $data['term_option'] = $this->Restaurentmenus_model->get_res_menu(0,5,0,$config['total_rows'] );
            $res = new Restaurent();
            $data['list_res'] = $res->get();           
            $data['view'] = 'restaurentmenus_index';
            $this->load->view('back_end/template_noright',$data);
        }   
        // Thêm món ăn vào thực đơn
        function add($id = 0,$row = 0)
        {   
            // get restaurentmenu                                                                                         
            $term = new Term_taxonomy();
            $term->include_related('term', array('id', 'name','slug'))->get_by_term_id($id);
            $data['term'] = $term; 
            // Lay ra danh sach mon an cua thuc don
            $list_child_product = new Product();           
            //echo $term->term_id;
            if($term->term_id>0)
            {
                $list_child_product->include_related('term_taxonomy',array('term_id','taxonomy'))
                        ->where_related('term_taxonomy', 'taxonomy', 'restaurentmenu')
                        ->where_related('term_taxonomy', 'term_id', $term->term_id);  
                //$list_child_product->include_related('term_taxonomy_product',array('term_order'));                                   
            }
            $list_child_product->select('hd_term_taxonomies_products.term_order');
            $list_child_product->select('hd_term_taxonomies_products.id AS term_tax_product_id');
            $list_child_product->order_by('hd_term_taxonomies_products.term_order','ASC');
            $data['list_child_product'] = $list_child_product->get();
            // danh sach cac mon an cua nha hang
            $product = new Product(); 
            if ($term->restaurent_id >0 )$product->where('restaurent_id',$term->restaurent_id);   
            $product->where('status','open');           
            $data['list_product'] = $product->get();
            //echo $list_child_product->check_last_query();            
            $data['view'] = 'restaurentmenus_add';            
            $this->load->view('back_end/template_noright',$data);
        }
        // Cập nhật vị trí các món ăn trong thực đơn
        function change_order($id = 0)
        {
            // get restaurentmenu                                                                                         
            $term = new Term_taxonomy();
            $term->include_related('term', array('id', 'name','slug'))->get_by_term_id($id);
            $data['term'] = $term; 
            // Lay ra danh sach mon an cua thuc don
            $list_child_product = new Product();           
            //echo $term->term_id;
            if($term->term_id>0)
            {
                $list_child_product->include_related('term_taxonomy',array('term_id','taxonomy'))
                        ->where_related('term_taxonomy', 'taxonomy', 'restaurentmenu')
                        ->where_related('term_taxonomy', 'term_id', $term->term_id);  
                //$list_child_product->include_related('term_taxonomy_product',array('term_order'));                                   
            }
            $list_child_product->select('hd_term_taxonomies_products.term_order');
            $list_child_product->select('hd_term_taxonomies_products.id AS term_tax_product_id');
            $list_child_product->order_by('hd_term_taxonomies_products.term_order','ASC');
            $list_child_product = $list_child_product->get();
            //echo $list_child_product->check_last_query();
            // get list product
           
            // update order
            foreach($list_child_product as $product)
            {
                $term_tax_product_id = $this->input->post('term_tax_product_id'.$product->term_tax_product_id);            
                $order = $this->input->post('order'.$product->term_tax_product_id);
                $temp = new Term_taxonomy_product();
                $temp->get_by_id($term_tax_product_id);
                $temp->term_order = $order;
                $temp->save();
            }
            redirect('administrator/restaurentmenus/add/'.$id);
        }
        function delete()
        {
            if(($this->session->userdata('login')&& ($this->User_identity->check_acess('category.delete'))))
            {
                $id = $this->input->post('param');                                    
                $term_taxonomy = new Term_taxonomy();
                $term_taxonomy->where('term_id',$id)->get();
                $list_child = $this->Restaurentmenus_model->get($id,1);
                if (count($list_child)>0)            
                foreach( $list_child as $child)
                {
                   $temp_term =  new Term_taxonomy();
                   $temp_term->where('term_id', $child->id)->get();
                   $temp_term->parent_term = $term_taxonomy->parent_term;
                   $temp_term->save();
                }
                $term_taxonomy->delete();
                
                $term = new Term();
                $term->get_by_id($id);
                $term->delete();          
            }
        }
        function add_child_res_menu()
        {
            $id = $this->input->post('id');
            $object_id = $this->input->post('object_id');
            $product = new Product();
            $product->get_by_id($id);
            $term_taxonomy =  new Term_taxonomy();
            $term_taxonomy->where('term_id',$object_id)->get();
            $term_taxonomy->save($product);
            
            echo 'thanh cong';
        }
        function delete_child_res_menu()
        {
            $id = $this->input->post('id');
            $object_id = $this->input->post('object_id');
            $product = new Product();
            $product->get_by_id($id);
            $term_taxonomy =  new Term_taxonomy();
            $term_taxonomy->where('term_id',$object_id)->get();
            $term_taxonomy->delete($product);
            echo 'thanh cong';
        }
        function edit($id = 0,$row = 0)
        {
            if(!($this->session->userdata('login'))) redirect('administrator/login');
            if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.edit'))))
            redirect('administrator/index');
            
            $name = $this->input->post('txttitle');
            $restaurent_id = $this->input->post('restaurent_id');
            $slug = $this->input->post('txtslug');		
            $description = $this->input->post('txtexcerpt');
            $parent = $this->input->post('ddlTerm');
            if ($this-> input-> post('txttitle')){
                $id=$this->input->post('term_id');                
                $term = new Term();
                $term->where('id',$id)->get();
                $term->name = $name;
                $term->slug = $slug;                              
                $term->save();                
                $term_taxonomy =  new Term_taxonomy();
                $term_taxonomy->where('term_id', $id)->get();
                $term_taxonomy->description = $description;
                $term_taxonomy->restaurent_id = $restaurent_id;  
                $term_taxonomy->parent_term = $parent;
                $term_taxonomy->save();                
                redirect('administrator/restaurentmenus','refresh');
            }else
            {              
                                
                //print_r($term);
                include('paging.php');
                $config['base_url']= base_url()."administrator/restaurentmenus/index/";
                $config['total_rows']=$this->Restaurentmenus_model->get_count_res_menu(0,5);                      		
                $config['cur_page']= $row;
                $config['num_links'] = 3;		
                $this->pagination->initialize($config);
                $data['list_link'] = $this->pagination->create_links();	                    
                $data['list_res_menu'] = $this->Restaurentmenus_model->get_res_menu(0,5,$row,$config['per_page']);
                //print_r($data['list_res_menu']);
                $data['term_option'] = $this->Restaurentmenus_model->get_res_menu(0,5,0,$config['total_rows'] );
                $res = new Restaurent();
                $data['list_res'] = $res->get();
                      
                $term = new Term_taxonomy();
                $term->include_related('term', array('id', 'name','slug'))->get_by_term_id($id);
                $data['term'] = $term;
                     
                $data['view'] = 'restaurentmenus_edit';
                $this->load->view('back_end/template_noright',$data);
            }
        }
        function test()
        {
            $temp = new Term_taxonomy_product();
            $temp->get_by_id(11);
            $temp->term_order = 100;
            $temp->save();
            echo $temp->check_last_query();
        }
    }
?>
